package com.hit.aircraftwar.dao;


import android.content.Context;

import com.hit.aircraftwar.activity.MainActivity;
import com.hit.aircraftwar.setter.ParameterSet;

import java.io.*;
import java.util.LinkedList;
import java.io.FileInputStream;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @author LiuYuZhe
 */
public class RecordsDaoImpl implements RecordsDAO  {

    private String name;

    public RecordsDaoImpl() {
        if(ParameterSet.getInstance().getDifficulty() == 1) {
            this.name = ParameterSet.INSTANCE.getName() + "Records1.txt";
        } else if(ParameterSet.getInstance().getDifficulty() == 2) {
            this.name = ParameterSet.INSTANCE.getName() + "Records2.txt";
        } else {
            this.name = ParameterSet.INSTANCE.getName() + "Records0.txt";
        }
    }

    @Override
    public void setName() {
        if(ParameterSet.getInstance().getDifficulty() == 1) {
            name = ParameterSet.INSTANCE.getName() + "Records1.txt";
        } else if(ParameterSet.getInstance().getDifficulty() == 2) {
            name = ParameterSet.INSTANCE.getName() + "Records2.txt";
        } else {
            name = ParameterSet.INSTANCE.getName() +  "Records0.txt";
        }
    }

    @Override
    public LinkedList<Record> getAllRecord() throws IOException{
        LinkedList<Record> records = new LinkedList<>();
        FileInputStream fis = MainActivity.mainActivity.openFileInput(name);
//        File file = new File(name);
//        if(!file.exists()) {
//            return records;
//        }
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String str;
        while((str = br.readLine()) != null) {
            String[] recordLine = str.split(",");
            int score = Integer.parseInt(recordLine[1]);
            records.add(new Record(recordLine[0], score, recordLine[2]));
        }
        br.close();
        return records;
    }


    @Override
    public void addRecord(Record record) throws IOException{
        //用于保证文件已经创建
        FileOutputStream fos1 = MainActivity.mainActivity.openFileOutput(name,Context.MODE_APPEND);
        LinkedList<Record> records = getAllRecord();
        if(records == null) {
            records = new LinkedList<>();
            records.add(record);
        } else {
            int index = records.size();
            for(Record tmpRecord : records) {
                if(tmpRecord.getScore() < record.getScore()) {
                    index = records.indexOf(tmpRecord);
                    break;
                }
            }
            records.add(index, record);
        }
        FileOutputStream fos = MainActivity.mainActivity.openFileOutput(name,Context.MODE_PRIVATE);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        for(Record tmpRecord : records) {
            bw.write(tmpRecord.toString() + '\n');
        }
        bw.flush();
        bw.close();
    }

    @Override
    public void removeRecord(String date) throws IOException {
        LinkedList<Record> records = getAllRecord();
        for(Record tmpRecord : records) {
            if(tmpRecord.getTimeInfo().equals(date)) {
                records.remove(tmpRecord);
                break;
            }
        }
        FileOutputStream fos = MainActivity.mainActivity.openFileOutput(name,Context.MODE_PRIVATE);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        for(Record tmpRecord : records) {
            bw.write(tmpRecord.toString() + '\n');
        }
        bw.flush();
        bw.close();
    }
}
